package com.supermarket_project.controller;

import com.supermarket_project.common.R;
import com.supermarket_project.pojo.Category;
import com.supermarket_project.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


@CrossOrigin
@RestController
@RequestMapping("/supermarket/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/addCategory")
    public R addCategory(@RequestBody Category category){
        if (category.getCategoryName() == null){
            return R.error("类别名称不能为空");
        }
        return categoryService.addCategory(category);
    }

    @GetMapping("/primaryCategory")
    public R primaryCategory(){
        return categoryService.primaryCategory();
    }

    @GetMapping("/secondCategory")
    public R secondCategory(Integer categoryId){
        return categoryService.secondCategory(categoryId);
    }


    @GetMapping("/pageCategory")
    public R pageCategory(
            int pageNum,
            int pageSize,
            Integer fatherId,
            String primary,
            String second
    ){
        return categoryService.pageCategory(pageNum,pageSize,fatherId,primary,second);
    }

    @PutMapping("/updateCategory")
    public R updateCategory(@RequestBody Category category){
        return categoryService.updateCategory(category);
    }

    @PutMapping("/lockCategory/{categoryId}/{status}")
    public R lockCategory(@PathVariable("categoryId") int categoryId,
                          @PathVariable("status") int status){
        return categoryService.lockCategory(categoryId,status);
    }

    @GetMapping("/queryCategory")
    public R queryCategory(Integer categoryId){
        return categoryService.queryCategory(categoryId);
    }

    @GetMapping("/secondCategoryByPrimary")
    public R secondCategoryByPrimary(int fatherId){
        return categoryService.secondCategoryByPrimary(fatherId);
    }

    @GetMapping("/allCategory")
    public R allCategory(){
        return categoryService.allCategory();
    }
}
